package cn.wipace.actionlib.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.wipace.actionlib.inter.GameInter;
import cn.wipace.actionlib.model.UserScore;
import cn.wipace.actionlib.util.DataUtil;
import cn.wipace.actionlib.util.UserActionUtil;
import rorbin.q.radarview.RadarData;
import rorbin.q.radarview.RadarView;

public class RadarMapActivity extends Activity {

    private RadarView mRadarView;
    private UserScore mUserScore;
    private String pn;
    private Button mBtnReplay;
    private Button mBtnQuit;
    int radarViewId = 0;
    int btnReplayId = 0;
    int btnQuitId = 0;
    int tvSportId = 0;
    int tvMindId = 0;
    int tvEmergencyId = 0;
    int tvCoorderId = 0;
    private TextView mTvSportScore;
    private TextView mTvMindScore;
    private TextView mTvEmergencyScore;
    private TextView mTvCoorderScore;
    public static GameInter mGameInter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pn = getIntent().getStringExtra("pn");
        mUserScore = (UserScore) getIntent().getSerializableExtra("score");
        int layoutId = UserActionUtil.getResourceIdByName(pn, "layout", "activity_radar_map");
        setContentView(layoutId);
        radarViewId = UserActionUtil.getResourceIdByName(pn, "id", "radarView");
        btnReplayId = UserActionUtil.getResourceIdByName(pn, "id", "id_btn_replay");
        btnQuitId = UserActionUtil.getResourceIdByName(pn, "id", "id_btn_quit");
        mRadarView = (RadarView) findViewById(radarViewId);
        mBtnReplay = (Button) findViewById(btnReplayId);
        mBtnQuit = (Button) findViewById(btnQuitId);

        tvSportId = UserActionUtil.getResourceIdByName(pn, "id", "id_tv_sport");
        tvMindId = UserActionUtil.getResourceIdByName(pn, "id", "id_tv_mind");
        tvEmergencyId = UserActionUtil.getResourceIdByName(pn, "id", "id_tv_emergency");
        tvCoorderId = UserActionUtil.getResourceIdByName(pn, "id", "id_tv_coorder");

        mTvSportScore = (TextView) findViewById(tvSportId);
        mTvMindScore = (TextView) findViewById(tvMindId);
        mTvEmergencyScore = (TextView) findViewById(tvEmergencyId);
        mTvCoorderScore = (TextView) findViewById(tvCoorderId);

        setListener();
        initDatas();
    }

    private void setListener() {

        mBtnReplay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                if (mGameInter != null) {
                    mGameInter.startGame();
                }
            }
        });
        mBtnQuit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                if (mGameInter != null) {
                    mGameInter.exitSystemt();
                }
            }
        });

    }

    private void initDatas() {
        if (mUserScore == null) {
            mRadarView.setEmptyHint("无数据");
            return;
        }
        List<Integer> layerColor = new ArrayList<>();
        List<String> vertexText = new ArrayList<>();
        List<Float> values = new ArrayList<>();
        List<String> names = new ArrayList<>();

        float mind = mUserScore.getMindFullness();
        float exer = mUserScore.getExerciseAbility();
        float coor = mUserScore.getCoordination();
        float emer = mUserScore.getEmergencyResponse();

        Collections.addAll(layerColor, 0x3300bcd4, 0x3303a9f4, 0x335677fc, 0x333f51b5, 0x33673ab7);
        Collections.addAll(values, mind, exer, coor, emer);
        Collections.addAll(vertexText, "专注能力", "运动能力", "协调能力", "反应能力");
        Collections.addAll(names, DataUtil.formatSocre(mind), DataUtil.formatSocre(exer), DataUtil.formatSocre(coor), DataUtil.formatSocre(emer));

        mTvMindScore.setText(DataUtil.formatSocre(mind) + "分");
        mTvSportScore.setText( DataUtil.formatSocre(exer) + "分");
        mTvCoorderScore.setText(DataUtil.formatSocre(coor)+ "分");
        mTvEmergencyScore.setText(DataUtil.formatSocre(emer) + "分");


        mRadarView.setLayerColor(layerColor);//设置雷达各层的颜色
        mRadarView.setVertexText(vertexText);//设置顶点的文字内容
        mRadarView.setMaxValue(100);

        RadarData data = new RadarData(values);
        data.setValueTextEnable(true);//设置数据是否显示描述
        data.setValueText(names);//设置数据值的描述
        data.setVauleTextColor(Color.parseColor("#000000"));
        data.setValueTextSize(12);//设置数据值的文字大小
        data.setLabel("能力雷达图");
        mRadarView.addData(data);
    }

    public static GameInter getmGameInter() {
        return mGameInter;
    }

    public static void setmGameInter(GameInter mGameInter) {
        RadarMapActivity.mGameInter = mGameInter;
    }

}
